<?php 

/**
*Form Builder & Admin Generator

*@author marcio <opi14@op.pl>, <polishvodka7@gmail.com>
*@copyright Copyright (c) 2011, marcio
*@version 1.6.5
*/

require_once('abstract/Validation.php');
require_once('interfaces/IValidation.php');

class str extends Volta_Validation implements IValidation
{

	/**
	*Skladowa klasy ktora przechowywuje tresci bledow
	*@access protected
	*/
	protected $error = array(
							'pl' => array(
							
								'required' => 'Pole %field% nie moze byc puste',
								'min' => 'Pole %field% jest zakrotkie',
								'max' => 'Pole %field% jest zadlugie',
								'between' => 'Pole %field% nie miesci sie w zakresie dlugosci',
								'alpha' => 'Pole %field% moze zawierac tylko litery',
								'digit' => 'Pole %field% moze zawierac tylko liczby',
								'alphadigit' => 'Pole %field% moze zawierac tylko znaki alfanumeryczne'
								
							)		
							);
	
	
	/**
	*Konstruktor ustawia konfiguracje walidatora
	*@access public 
	*@param array $cfg
	*/
	public function __construct($cfg)
	{
		parent::__construct();
		$this -> configure($cfg);
	}
	
	
	/**
	*Metoda ktora sprawdza walidacje danych na podstawie wczesniej ustawionej konfiguracji
	*@access public 
	*@param string $object tresc do walidacji
	*@return bool|string
	*/
	public function is_valid($object)
	{	
		if($this -> get_option('required'))
		{
			$message = $this -> error[$this -> language -> getLang()]['required'];
			
			if(empty($object))
				return $this -> set_error($message, $this -> get_option('field'));
		}
		
		
		if($this -> get_option('min') && strlen($object) < $this -> get_option('min'))
		{
			$message = $this -> error[$this -> language -> getLang()]['min'];
			return $this -> set_error($message, $this -> get_option('field'));
		}

		
		if($this -> get_option('max') && strlen($object) > $this -> get_option('max'))
		{
			$message = $this -> error[$this -> language -> getLang()]['max'];
			return $this -> set_error($message, $this -> get_option('field'));
		}
		
		
		if($this -> get_option('between'))
		{
			$between = $this -> get_option('between');
	
			if((strlen($object) < $between[0]) || (strlen($object) > $between[1]))
			{
				$message = $this -> error[$this -> language -> getLang()]['between'];
				return $this -> set_error($message, $this -> get_option('field'));
			}
		}
		
		
		if($this -> get_option('digit'))
		{
			if(preg_match('/^[0-9]/', $object))
			{
				$message = $this -> error[$this -> language -> getLang()]['digit'];
				return $this -> set_error($message, $this -> get_option('field'));
			}
		}
		
		
		if($this -> get_option('alpha'))
		{
			if(preg_match('/^[a-zA-Z\-\_\@]/', $object))
			{
				$message = $this -> error[$this -> language -> getLang()]['alpha'];
				return $this -> set_error($message, $this -> get_option('field'));
			}
		}
		
		
		if($this -> get_option('alphadigit'))
		{
			if(!ctype_alnum($object))
			{
				$message = $this -> error[$this -> language -> getLang()]['alphadigit'];
				return $this -> set_error($message, $this -> get_option('field'));
			}
		}
		
		return true;
	}
}

?>